package com.taiyi.renaissancemall.ui.classification;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ActivityUtils;
import com.taiyi.renaissancemall.bean.FirstClassBean;
import com.taiyi.renaissancemall.ui.classification.goodlist.GoodListActivity;

public class FirstClassAdapter extends BaseAdapter {

    private FirstClassBean firstClassBean;
    private LayoutInflater inflater;
    private clickFirstClassListener clickFirstClassListener;

    public FirstClassAdapter(FirstClassBean firstClassBean, Context context) {
        this.firstClassBean = firstClassBean;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return firstClassBean.classificationlist.size();
    }

    @Override
    public Object getItem(int i) {
        return firstClassBean.classificationlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.class_item_tv_layout, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.tv_title = convertView.findViewById(R.id.tv_title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        String title = firstClassBean.classificationlist.get(i).name;
        String title_id = firstClassBean.classificationlist.get(i).classificationid;
        viewHolder.tv_title.setText(title);
        viewHolder.tv_title.setSelected(firstClassBean.classificationlist.get(i).isSelected);
        viewHolder.tv_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (clickFirstClassListener != null) {
                    clickFirstClassListener.showNextClass(title_id, i);
                }
                for (int i1 = 0; i1 < firstClassBean.classificationlist.size(); i1++) {
                    if (i == i1)
                        firstClassBean.classificationlist.get(i1).isSelected = true;
                    else firstClassBean.classificationlist.get(i1).isSelected = false;
                }
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView tv_title;
    }

    interface clickFirstClassListener {
        void showNextClass(String id, int position);
    }

    public void setClickFirstClassListener(clickFirstClassListener clickFirstClassListener) {
        this.clickFirstClassListener = clickFirstClassListener;
    }

}
